<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: version_upgrade.php 4 2008-08-03 01:43:03Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * Installation template class
 *
 * @package Fusion Registry
 * @subpackage installer
 * @version $Id: version_upgrade.php 4 2008-08-03 01:43:03Z registry.fusion $
 */
if ( ! defined( 'IN_IPB' ) )
{
    print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
    exit();
}

class version_upgrade
{
    var $ipsclass;
    var $instagrade;
    var $this_version = '30002';
    var $upgrade_from = '30001';
    var $base_url     = '';
    var $mod_to_run   = '';


    /**
     * Generate the upgrades to happen string
     */
    function version_process()
    {
        $this->base_url  = "index.php?act=kit";

        if ( is_array( $this->instagrade->modules_to_run ) and count( $this->instagrade->modules_to_run ) )
        {
            $tmp = array_shift( $this->instagrade->modules_to_run );

            $this->mod_to_run = implode( ', ', $this->instagrade->modules_to_run );
        }

        if ( ! $this->mod_to_run )
        {
            $this->mod_to_run = 'None';
        }
    }


    /**
     * Oh how we love to upgrade!
     */
    function auto_run()
    {
        switch( $this->ipsclass->input['kitact'] )
        {
            case 'cleanup':
                $this->do_cleanup();
                break;

            default:
                $this->upgrade_kit_intro();
                break;
        }
    }



    /**
     * Only thing we need to do is update the DB version
     */
    function do_cleanup()
    {
        $executed = 0;

        // Update the database version strings
        $vers = array( 'version_long' => '30002',
                       'version_short' => 'v3.0.2' );

        foreach ( $vers as $name => $value )
        {
            $db_string = $this->ipsclass->DB->compile_db_update_string( array( 'config_name' => $name,
                                                                               'config_value' => $value
                                                                      )      );

            $this->ipsclass->DB->query( "UPDATE ".$this->ipsclass->vars['sql_tbl_prefix']."registry_config SET $db_string ".
                                        "WHERE config_name='$name'" );

            $executed++;
        }


        // All done
		$this->instagrade->redirect( "index.php?act=done", "Cleanup complete, $executed queries run...." );
    }


    /**
     * Print intro
     */
    function upgrade_kit_intro()
    {
        $this->ipsclass->template->print_header();

        $this->ipsclass->template->contents .= <<<HTML
This upgrade kit will upgrade you from <b>"{$this->instagrade->version_history[$this->upgrade_from]}"</b> to <b>"{$this->instagrade->version_history[$this->this_version]}"</b>
<br />
<br />
<div align='center'><a href="{$this->base_url}&kitact=cleanup" class='button' style='color: #003366'>Continue >></a></div>
<br />
<br />
<div align='center'>Upgrade kits still to run after this upgrade: {$this->mod_to_run}</div>
HTML;

        $this->ipsclass->template->output();
    }

}

?>